//
//  HPCustomerInfoView.m
//  HappyPenguin
//
//  Created by zhuangyihang on 1/10/16.
//  Copyright © 2016 zhuangyihang. All rights reserved.
//

#import "HPCustomerInfoView.h"
#import "UIConstants.h"
#import "AppHelper.h"

static const NSInteger kNumberLabelHeight = 40;

static const NSInteger kNameLabelMarginLeft = 5;
static const NSInteger kNameLabelMarginBottom = 10;
static const NSInteger kGenderLabelMarginLeft = 10;
static const NSInteger kPortraitViewWidth = 100;
static const NSInteger kPortraitViewMarginBottom = 10;


@interface CustomerInfoLabel : UIView

@property (nonatomic, strong) UILabel *topLabel;
@property (nonatomic, strong) UILabel *bottomLabel;

@end

@implementation CustomerInfoLabel

- (id)init{
    self = [super init];
    if (self) {
        self.topLabel = [UILabel new];
        [self addSubview:self.topLabel];
        [self.topLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self);
            make.bottom.equalTo(self.mas_centerY);
        }];
        
        self.bottomLabel = [UILabel new];
        [self addSubview:self.bottomLabel];
        [self.bottomLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self);
            make.top.equalTo(self.mas_centerY);
        }];
        
        self.topLabel.font = [UIConstants getFont:15];
        self.bottomLabel.font = [UIConstants getFont:11];
        
        self.topLabel.textColor = [UIColor whiteColor];
        self.bottomLabel.textColor = [UIColor whiteColor];
        
    }
    return self;
}

@end


@interface HPCustomerInfoView()

@property (nonatomic, strong) UIImageView *portraitImageView;
@property (nonatomic, strong) UILabel *nameLabel;
@property (nonatomic, strong) UILabel *genderLabel;

@property (nonatomic, strong) CustomerInfoLabel *feeLabel;
@property (nonatomic, strong) CustomerInfoLabel *purchaseTimesLabel;
@property (nonatomic, strong) CustomerInfoLabel *noPurchaseDaysLabel;

@end

@implementation HPCustomerInfoView

- (id)init{
    self = [super init];
    if (self) {
        
        UIImageView *v = [UIImageView new];
        [self addSubview:v];
        [v mas_makeConstraints:^(MASConstraintMaker *make) {
            make.size.equalTo(self);
            make.center.equalTo(self);
        }];
        v.image = [UIImage imageNamed:@"Customer_PanelBackGround"];
        v.contentMode = UIViewContentModeScaleAspectFill;
        
        self.feeLabel = [CustomerInfoLabel new];
        [self addSubview:self.feeLabel];
        self.feeLabel.bottomLabel.text = @"消费金额";
        
        self.purchaseTimesLabel = [CustomerInfoLabel new];
        [self addSubview:self.purchaseTimesLabel];
        self.purchaseTimesLabel.bottomLabel.text = @"消费次数";
        
        self.noPurchaseDaysLabel = [CustomerInfoLabel new];
        [self addSubview:self.noPurchaseDaysLabel];
        self.noPurchaseDaysLabel.bottomLabel.text = @"未消费金额";
        
        [self.feeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self);
            make.right.equalTo(self.purchaseTimesLabel.mas_left);
            make.height.mas_equalTo(kNumberLabelHeight);
            make.bottom.equalTo(self);
            make.width.equalTo(self).multipliedBy(1/3.0);
        }];
        
        [self.purchaseTimesLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(kNumberLabelHeight);
            make.left.equalTo(self.feeLabel.mas_right);
            make.right.equalTo(self.noPurchaseDaysLabel.mas_left);
            make.bottom.equalTo(self);
            make.width.equalTo(self).multipliedBy(1/3.0);
        }];
        
        [self.noPurchaseDaysLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(kNumberLabelHeight);
            make.left.equalTo(self.purchaseTimesLabel.mas_right);
            make.right.equalTo(self);
            make.bottom.equalTo(self);
            make.width.equalTo(self).multipliedBy(1/3.0);
        }];
        
        self.feeLabel.topLabel.text = @"0";
        self.purchaseTimesLabel.topLabel.text = @"0";
        self.noPurchaseDaysLabel.topLabel.text = @"0";
        
        
        self.portraitImageView = [UIImageView new];
        [self addSubview:self.portraitImageView];
        self.portraitImageView.layer.borderColor = [UIColor whiteColor].CGColor;
        self.portraitImageView.layer.cornerRadius = kPortraitViewWidth*[AppHelper scaleForI6P]/2;
        self.portraitImageView.layer.borderWidth = 1;
        self.portraitImageView.clipsToBounds = YES;
        self.portraitImageView.contentMode = UIViewContentModeScaleAspectFill;
        
        self.nameLabel = [UILabel new];
        [self addSubview:self.nameLabel];
        self.nameLabel.font = [UIConstants getFont:16];
        self.nameLabel.textColor = [UIColor whiteColor];
        
        self.genderLabel = [UILabel new];
        [self addSubview:self.genderLabel];
        self.genderLabel.font = [UIConstants getFont:11];
        self.genderLabel.textColor = [UIColor whiteColor];

        [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(self.feeLabel.mas_top).offset(-kNameLabelMarginBottom);
            make.left.equalTo(self.portraitImageView).offset(kNameLabelMarginLeft);
        }];
        

        [self.genderLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(self.feeLabel.mas_top).offset(-kNameLabelMarginBottom);
            make.left.equalTo(self.nameLabel.mas_right).offset(kGenderLabelMarginLeft);
        }];
        
        [self.portraitImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.height.mas_equalTo(kPortraitViewWidth*[AppHelper scaleForI6P]);
            make.centerX.equalTo(self);
            make.bottom.equalTo(self.nameLabel.mas_top).offset(-kPortraitViewMarginBottom);
        }];
        
        UIView *sep = [UIView new];
        [self addSubview:sep];
        [sep mas_makeConstraints:^(MASConstraintMaker *make) {
            make.height.equalTo(@15);
            make.width.equalTo(@1);
            make.centerY.equalTo(self.noPurchaseDaysLabel);
            make.left.equalTo(self.feeLabel.mas_right);
        }];
        sep.backgroundColor = [UIColor colorWithRGB:0xfefefe];
        
        sep = [UIView new];
        [self addSubview:sep];
        [sep mas_makeConstraints:^(MASConstraintMaker *make) {
            make.height.equalTo(@15);
            make.width.equalTo(@1);
            make.centerY.equalTo(self.noPurchaseDaysLabel);
            make.left.equalTo(self.purchaseTimesLabel.mas_right);
        }];
        sep.backgroundColor = [UIColor colorWithRGB:0xfefefe];
    }
    return self;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/


- (void)setName:(NSString *)name{
    self.nameLabel.text = name;
}

- (void)setGender:(NSString *)gender{
    self.genderLabel.text = gender;
}

- (void)setPortrait:(NSString *)portrait{
    [self.portraitImageView sd_setImageWithURL:[NSURL URLWithString:portrait] placeholderImage:[UIConstants defaultPortrait]];
}

- (void)setFee:(NSInteger)fee{
    self.feeLabel.topLabel.text = [NSString stringWithFormat:@"%ld",(long)fee];
}

- (void)setPurchaseTimes:(NSInteger)purchaseTimes{
    self.purchaseTimesLabel.topLabel.text = [NSString stringWithFormat:@"%ld",(long)purchaseTimes];
}

- (void)setNoPurchaseDays:(NSInteger)noPurchaseDays{
    self.noPurchaseDaysLabel.topLabel.text = [NSString stringWithFormat:@"%ld",(long)noPurchaseDays];
}

@end
